﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{

}
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="//unpkg.com/layui@2.10.3/dist/css/layui.css" rel="stylesheet">
</head>

<body>
    <form class="layui-form" action="" id="baseStoreForm">
    </form>
    <!-- 表单内容保持不变... -->
    <table class="layui-hide" id="ID-table-demo-filter"></table>
    <script src="//unpkg.com/layui@2.10.3/dist/layui.js"></script>

    <!-- 操作列按钮模板 -->
    <script type="text/html" id="tableToolbar">
        <a class="layui-btn layui-btn-xs" lay-event="agree">同意</a>
        <a class="layui-btn layui-btn-xs" lay-event="reject">驳回</a>
    </script>

    <script>
        layui.use(['form', 'table', 'laydate', 'layer'], function () {
            var $ = layui.$;
            var form = layui.form;
            var table = layui.table;
            var laydate = layui.laydate;
            var layer = layui.layer;

            // 初始化日期选择器
            laydate.render({ elem: '#begindate', type: 'date' });
            laydate.render({ elem: '#enddate', type: 'date' });

            table.render({
                elem: '#ID-table-demo-filter',
                toolbar: 'default',
                height: 315,
                cols: [[
                    { field: 'addroveid', title: '序号', width: 120 },
                    { field: 'approval', title: '审批流程名称', width: 150, fixed: 'left' },
                    { field: 'process', title: '审批流程', width: 120 },
                    { field: 'roleName', title: '角色', width: 120 },
                    { field: 'userid', title: '用户id', width: 120 },
                    { field: 'userNiken', title: '用户昵称', width: 120 },
                    { field: 'appstatus', title: '状态', width: 120 },
                    { field: 'belongto', title: '意见', width: 120 },
                    { field: 'appDate', title: '操作时间', width: 120 },
                    {
                        field: 'operation',
                        title: '操作',
                        width: 150,
                        fixed: 'right',
                        toolbar: '#tableToolbar'
                    }
                ]],
                done: function () {
                    // 记录筛选状态
                    var that = this;
                    that.elem.next().on('mousedown', 'input[lay-filter="LAY_TABLE_TOOL_COLS"]+', function () {
                        var input = $(this).prev()[0];
                        layui.data('table-filter-test', {
                            key: input.name,
                            value: input.checked
                        });
                    });
                },
            });

            function loadRoleData() {
                layer.load(2);
                $.ajax({
                    url: 'http://localhost:5234/api/WorkOrder/GetApproval',
                    // url:"@ViewBag.BaseUrl/T8/WorkOrder/WorkOrder",
                    type: 'get',
                    dataType: 'json',
                    success: function (res) {
                        layer.closeAll('loading');
                        if (res.code === 210) {
                            table.reload('ID-table-demo-filter', {
                                data: res.data,
                            });
                        } else {
                            layer.msg('获取数据失败: ' + (res.message || '未知错误'), { icon: 2 });
                        }
                    },
                    error: function (xhr, status, error) {
                        layer.closeAll('loading');
                        layer.msg('请求失败: ' + error, { icon: 2 });
                    }
                });
            }

            // 监听查询按钮提交事件
            form.on('submit(search)', function (data) {
                loadRoleData();
                return false;
            });

            // 初始加载数据
            loadRoleData();

            // 监听表格操作列按钮点击事件
            table.on('tool(ID-table-demo-filter)', function (obj) {
                var data = obj.data;
                if (obj.event === 'agree') {
                    var addroveid = data.addroveid;
                    var belongto = data.belongto;  // 从数据中获取意见，而不是初始化为空字符串
                    var userid = data.userid;  // 统一参数名，这里都用小写userid
                    console.log('序号: ', addroveid);
                    console.log('意见: ', belongto);
                    console.log('用户 ID: ', userid);

                    layer.confirm('确定要同意该审批吗？', function (index) {
                        layer.load(2);
                        $.ajax({
                            url: 'http://localhost:5090/api/WrokOrder/PutApproval', // 替换为实际同意接口地址
                            type: 'post',
                            // 确保传递的是变量的值，而不是字符串 "belongto"
                            data: { addroveid: addroveid, userid: userid, belongto: belongto },
                            dataType: 'json',
                            contentType: 'application/json', // 明确设置内容类型为 JSON
                            success: function (res) {
                                layer.closeAll('loading');
                                if (res.code === 206) {
                                    layer.msg('同意成功', { icon: 1 });
                                    loadRoleData(); // 刷新表格数据
                                } else {
                                    layer.msg('同意失败: ' + (res.message || '未知错误'), { icon: 2 });
                                }
                            },
                            error: function (xhr, status, error) {
                                layer.closeAll('loading');
                                layer.msg('请求失败: ' + error, { icon: 2 });
                            }
                        });
                        layer.close(index);
                    });
                } else if (obj.event === 'reject') {
                    layer.open({
                        type: 1,
                        title: '填写驳回意见',
                        content: '<div style="padding: 20px;">' +
                            '<textarea id="belongto" placeholder="请输入驳回意见" class="layui-textarea"></textarea>' +
                            '</div>',
                        area: ['400px', '250px'],
                        btn: ['确定', '取消'],
                        yes: function (index, layero) {
                            var belongto = $('#belongto').val();
                            if (belongto === '') {
                                layer.msg('请输入驳回意见', { icon: 2 });
                                return;
                            }
                            layer.load(2);
                            $.ajax({
                                url: 'http://localhost:5090/api/WrokOrder/PutApproval', // 替换为实际驳回接口地址
                                type: 'post',
                                // 确保传递的是变量的值，而不是字符串 "belongto"
                                data: { addroveid: data.addroveid, belongto: belongto, userid: data.userid },
                                dataType: 'json',
                                contentType: 'application/json', // 明确设置内容类型为 JSON
                                success: function (res) {
                                    layer.closeAll('loading');
                                    if (res.code === 206) {
                                        layer.msg('驳回成功', { icon: 1 });
                                        loadRoleData(); // 刷新表格数据
                                    } else {
                                        layer.msg('驳回失败: ' + (res.message || '未知错误'), { icon: 2 });
                                    }
                                },
                                error: function (xhr, status, error) {
                                    layer.closeAll('loading');
                                    layer.msg('请求失败: ' + error, { icon: 2 });
                                }
                            });
                            layer.close(index);
                        },
                        btn2: function (index, layero) {
                            layer.close(index);
                        }
                    });
                }
            });
        });
    </script>
</body>
</html>